package com.cnhis.cloudhealth.clinical.medical.base.bo;


import com.cnhis.cloudhealth.clinical.medical.base.dao.MedicalVef1Dao;
import com.cnhis.cloudhealth.clinical.medical.base.dao.MpatientInfoDao;
import com.cnhis.cloudhealth.clinical.medical.base.dao.MperformDao;
import com.cnhis.cloudhealth.clinical.medical.base.mapper.MedicalVef1;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.cardmember.bo.CardMemberMangerBo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

@Service
public class MpatientInfoBo {
    @Resource
    private MpatientInfoDao mpatientInfoDao;

    @Resource
    private MperformDao mperformDao;
    @Autowired
    private CardMemberMangerBo cardMemberMangerBo;
    @Autowired
    private MedicalVef1Dao medicalVef1Dao;
    @Autowired
    private GetNewId getNewId;

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人列表)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> findPatientList(Map<String, Object> map) throws Exception {
        return mpatientInfoDao.findPatientList(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人详细信息)
     * @author chengshaowei
     * @date 2016/9/19 10:33
     */
    public Map<String, Object> findPatientInfo(Map map) throws Exception {
        return mpatientInfoDao.findPatientInfo(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人详细信息住院)
     * @author chengshaowei
     * @date 2016/9/19 10:33
     */
    public Map<String, Object> findHosPatientInfo(Map map) throws Exception {
        return mpatientInfoDao.findHosPatientInfo(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人医嘱内容 門診)
     * @author chengshaowei
     * @date 2016/9/19 10:33
     */
    public Map<String, Object> findOutAdvice(Map map) throws Exception {
        return mpatientInfoDao.findOutAdvice(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人医嘱内容 住院)
     * @author chengshaowei
     * @date 2016/9/19 10:33
     */
    public Map<String, Object> findHosAdvice(Map map) throws Exception {
        return mpatientInfoDao.findHosAdvice(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技项目对应收费项目记录 门诊)
     * @author chengshaowei
     * @date 2016/9/19 10:37
     */
    public Map<String, Object> findOutCharge(Map map) throws Exception {
        return mpatientInfoDao.findOutCharge(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技项目对应收费项目记录 住院)
     * @author chengshaowei
     * @date 2016/9/19 10:37
     */
    public Map<String, Object> findHosCharge(Map map) throws Exception {
        return mpatientInfoDao.findHosCharge(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技项目执行明细记录)
     * @author chengshaowei
     * @date 2016/9/19 10:37
     */
    public Map<String, Object> findDetail(Map map) throws Exception {
        return mpatientInfoDao.findDetail(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(查询科室信息)
     * @author chengshaowei
     * @date 2016/9/19 10:37
     */
    public Map<String, Object> findDepartment(Map map) throws Exception {
        return mpatientInfoDao.findDepartment(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(查询参数信息)
     * @author chengshaowei
     * @date 2016/9/19 10:37
     */
    public Map<String, Object> findParameters(Map map) throws Exception {
        return mpatientInfoDao.findParameters(map);
    }


    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技病人列表，通过费用查询)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> findMcostList(Map map) throws Exception {
        return mpatientInfoDao.findMcostList(map);
    }


    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技病人列表，混合模式查询，医嘱+费用 0)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> findMhybridList(Map map) throws Exception {
        return mpatientInfoDao.findMhybridList(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(医技病人列表，混合模式查询，医嘱+费用 other)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> findOtherList(Map map) throws Exception {
        return mpatientInfoDao.findOtherList(map);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(修改附属医嘱费用明细状态)Techic_VAJ1_Update
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> costDetail(Map<String, Object> map) throws Exception {
        Map<String, Object> msg = new HashMap<String, Object>();
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
        Integer ACF01 = GlobalVariable.IntegerIsNull(map.get("acf01"));
        ModelVo mv = new ModelVo();
        List<MedicalVef1> medicalVef1s = new ArrayList<>();
        List<Map<String, Object>> veg1addList = new ArrayList<>();
        List<Map<String, Object>> veg1upList = new ArrayList<>();
        List<Map<String, Object>> veg2addList = new ArrayList<>();
        List<Map<String, Object>> veg2upList = new ArrayList<>();
        for (Map<String, Object> m : list) {
            //查询门诊病人记账费用是否执行
            if (ACF01 == 1) {
                Map<String, Object> vaj1_map = mpatientInfoDao.findVAJ1(m);
                if (vaj1_map != null) {
                    msg.put("falg", false);
                    msg.put("msg", "收费明细已执行");
                    return msg;
                }
            }
            //查询住院病人记账费用是否执行
            if (ACF01 == 2) {
                Map<String, Object> vaj2_map = mpatientInfoDao.findVAJ2(m);
                if (vaj2_map != null) {
                    msg.put("falg", false);
                    msg.put("msg", "收费明细已执行");
                    return msg;
                }
            }
            m.put("vbi35", GlobalVariable.BigDecimalNull(m.get("vbi35")).subtract(GlobalVariable.BigDecimalNull(m.get("vbj05")))); //当前次数减本次次数
            m.put("vbi13", "3");//正在执行
            if (GlobalVariable.BigDecimalNull(m.get("vbi35")).compareTo(new BigDecimal(0)) == 0) {
                m.put("vbi13", "1");//执行完成   当当前次数减本次次数为0时
            }
            m.put("vaa01", map.get("vaa01"));
            m.put("vbj08", new Date());
            m.put("vbj10", new Date());
            if (ACF01 == 1) {
                //查询住门诊用明细是否作废
                List<Map<String, Object>> invalid_list = mpatientInfoDao.findInvalid(m);
                if (invalid_list != null && invalid_list.size() > 0) {
                    msg.put("falg", false);
                    msg.put("msg", "收费明细已经作废，不能在执行.");
                    return msg;
                }
                if ("0".equals(map.get("RateMode").toString())) {/**判断是否需要扣费**/
                    List<Map<String, Object>> acclist = mperformDao.findAccurately(m);
                    if (acclist != null && acclist.size() > 0) {
                        //查询是否是会员
                        Map<String, Object> members = mperformDao.findMembers(m);
                        if (members != null) {
                            msg.put("falg", false);
                            msg.put("msg", "病人不是会员，请先到收费处交钱");
                            return msg;
                        } else {
                            mv = deduction(map, m);
                            if (StaticKeys.OH_FALSE.equals(mv.getResult())) {
                                msg.put("falg", false);
                                msg.put("msg", mv.getResultMsg());
                                return msg;
                            }
                        }
                    }
                }
                Map<String, Object> map_veg1 = new HashMap<>();
                //判断是否存在
                Map<String, Object> mapveg1 = mperformDao.findVeg01(m);
                map_veg1.put("vaj01", m.get("vaj01"));
                map_veg1.put("veg02", 0);
                map_veg1.put("veg03", m.get("vbi35"));
                if (mapveg1 != null) {
                    veg1upList.add(map_veg1);
                } else {
                    veg1addList.add(map_veg1);
                }

            } else {
                Map<String, Object> map_veg2 = new HashMap<>();
                //判断是否存在
                Map<String, Object> mapveg2 = mperformDao.findVeg2(m);
                map_veg2.put("vaj01", m.get("vaj01"));
                map_veg2.put("veg02", 0);
                map_veg2.put("veg03", m.get("vbi35"));
                if (mapveg2 != null) {
                    veg2upList.add(map_veg2);
                } else {
                    veg2addList.add(map_veg2);
                }
            }
            addMedicalVef1(ACF01, map, m, medicalVef1s);

        }

        if (medicalVef1s != null && medicalVef1s.size() > 0) {
            medicalVef1Dao.batchSave(medicalVef1s);
        }
        if (ACF01 == 1) {
            if (veg1addList != null && veg1addList.size() > 0) {
                mperformDao.batchinsertByVeg1(veg1addList);
            }
            if (veg1upList != null && veg1upList.size() > 0) {
                mperformDao.batchupdateByVoVeg1(veg1addList);
            }
            mpatientInfoDao.updateCostVaj1(list);
        } else {
            if (veg2addList != null && veg2addList.size() > 0) {
                mperformDao.batchinsertByVeg2(veg2addList);
            }
            if (veg2upList != null && veg2upList.size() > 0) {
                mperformDao.batchupdateByVoVeg2(veg2addList);
            }
            mpatientInfoDao.updateCostVaj2(list);
        }
        /**批量保存医嘱记录**/
        msg.put("falg", true);
        msg.put("msg", "执行完成");
        return msg;
    }

    public ModelVo deduction(Map<String, Object> map, Map<String, Object> m) throws Exception {
        Map<String, Object> cardMap = new HashMap<String, Object>();
        cardMap.put("VAA01", GlobalVariable.LongIsNull(map.get("vaa01")));//病人ID
        cardMap.put("VAA07", GlobalVariable.LongIsNull(map.get("vaa07")));//就诊ID
        cardMap.put("VAI01", GlobalVariable.LongIsNull(m.get("vai01")));//单据ID,如果传入当前记账ID为记账状态，则不再扣费
        cardMap.put("ACF01", 1);//服务对象 1=门诊、2=住院
        cardMap.put("BCE01", map.get("BCE01"));//操作员id
        cardMap.put("BCK01", map.get("BCK01"));//科室id
        cardMap.put("BCE02", map.get("BCE02"));//操作员code
        cardMap.put("BCE03", map.get("BCE03"));//操作员姓名
        cardMap.put("VAJ38", m.get("vaj38"));//基本账户扣费
        cardMap.put("AType", 0);//0 = 扣费,1 = 退费
        cardMap.put("AWorkType", 4);//1=门诊记账，2=药房发药，3=医嘱记账，4=医技执行,5=挂号，6=门诊结账 , 7=住院结帐,8=简易挂号
        return cardMemberMangerBo.updatehoratecardcharge(cardMap);
    }

    public void addMedicalVef1(Integer ACF01, Map<String, Object> map, Map<String, Object> m, List<MedicalVef1> medicalVef1s) throws ParseException {
        MedicalVef1 vef1 = new MedicalVef1();
        vef1.setVef01(getNewId.nextId());
        vef1.setVaa01(GlobalVariable.LongIsNull(map.get("vaa01")));
        vef1.setVaa07(GlobalVariable.LongIsNull(map.get("vaa07")));
        vef1.setAcf01(ACF01);
        vef1.setVaj01(GlobalVariable.LongIsNull(m.get("vaj01")));
        if (m.get("vak13") != null) {
            if (StringUtils.isNotEmpty(m.get("vak13").toString())) {
                vef1.setVef06(DateUtils.String2Date(m.get("vak13").toString()));  //要求时间（收费时间）
            } else {
                vef1.setVef06(new Date());  //要求时间（收费时间）
            }
        }
        vef1.setVef07(GlobalVariable.BigDecimalNull(m.get("vbj05")));
        vef1.setVef08(GlobalVariable.BigDecimalNull(m.get("vbi35")));
        vef1.setVef09(GlobalVariable.StringIsNull(m.get("vbj06")));
        vef1.setBce03a(GlobalVariable.StringIsNull(m.get("bce03a")));
        vef1.setBce01a(GlobalVariable.IntegerIsNull(m.get("bce01a")));
        vef1.setVef12(new Date());
        vef1.setBce03b(GlobalVariable.StringIsNull(map.get("trueName")));
        vef1.setBce01b(GlobalVariable.IntegerIsNull(map.get("BCE01")));
        vef1.setVef15(new Date());
//            vef1.setBce03c();
//            vef1.setBce01c();
//            vef1.setBce03d();
//            vef1.setBce01d();
        vef1.setVef20(GlobalVariable.IntegerIsNull(map.get("vbj19")));
        vef1.setVef21(GlobalVariable.StringIsNull(map.get("vbj21")));
        medicalVef1s.add(vef1);
    }
    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(员工检索 other)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public Map<String, Object> findStaff(Map map) throws Exception {
        return mpatientInfoDao.findStaff(map);
    }

    /**
     * 院内消息查询
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> yuanNeiNotice(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return mpatientInfoDao.yuanNeiNotice(map, currPage, pageSize);
    }

    /**
     * chengshaowei 2017/8/15 14:10
     *
     * @param map
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Title: findSumMoney
     * @Description: （计算医嘱费用）
     */
    public Map<String, Object> findSumMoney(Map<String, Object> map) throws Exception {
        return mpatientInfoDao.findSumMoney(map);
    }
    
    /**
     * 查询已经扣过费用的项目名称
     * @Title: findYetItem   
     * @Description: TODO(方法功能描述)
     * @param: @param map
     * @param: @return
     * @param: @throws Exception
     * @return: Map<String,Object>      
     * @throws
     */
    public List<Map<String, Object>> findYetItem(Map<String, Object> map) throws Exception {
    	return mpatientInfoDao.findYetItem(map);
    }
}
